

#import "FileController.h"
#import "Imports.h"


@implementation FileController

-init
{	
	[super init];
	CurrentPanel=NULL;
	DoClose=YES;
	PanelCancelled=NO;
	DoQuit=NO;
	Quitting=NO;
	return self;
}

- New:sender
{
	[self NewRecordedSound:NULL];
	return self;
}

- NewRecordedSound: ThisSound
{
    /* Stolen right out of the NeXT Concepts 2.0, p. 9-33 */
	
	id theWindow, theScrollView, theSoundView, theSound;
	NXRect aRect, contentRect;
	
	[ThePreferencesManager SoundComingIn];
	[TheSoundTable NewWindowPos];
	NXSetRect(&aRect,[TheSoundTable WindowPosX],
					[TheSoundTable WindowPosY],600.0,250.0);
	theWindow=[Window newContent:&aRect
							style:NX_TITLEDSTYLE
							backing:NX_BUFFERED
							buttonMask:NX_ALLBUTTONS
							defer:NO];
	[theWindow setBackgroundGray:NX_WHITE];
	theScrollView=[ScrollView newFrame:&aRect];
	[theScrollView setVertScrollerRequired:NO];
	[theScrollView setHorizScrollerRequired:YES];
	
	[theWindow setContentView:theScrollView];
	contentRect=aRect;
	[ScrollView getContentSize:&(contentRect.size)
			forFrameSize:&(aRect.size)
			horizScroller:YES
			vertScroller:NO
			borderType:NX_NOBORDER];
	theSoundView = [UpdateSoundView newFrame:&contentRect];
		// my custom sound view.
	[theSoundView HeresTheEditController: TheEditController : [ThePreferencesManager ContinuousView]];
	[theSoundView notifyAncestorWhenFrameChanged:YES];
	
	if (ThisSound==NULL)
		{
		theSound = [[Sound alloc] init];
		}
	else
		{
		theSound=ThisSound;
		}
		
	[theSoundView setSound:theSound];
	[theSoundView setDisplayMode:[ThePreferencesManager MinMaxDisplay]];	[theScrollView setDocView:theSoundView];

	[[theScrollView superview] setAutoresizeSubviews:YES];
	[[theScrollView superview] setAutosizing:NX_HEIGHTSIZABLE|NX_WIDTHSIZABLE];
	[TheSoundTable NewItem:theSound:theSoundView:theWindow:"UNTITLED":YES];
	
	[theWindow setDelegate:self];
	[theWindow setTitle:"UNTITLED"];
	[theWindow setMiniwindowIcon: "SoundIcon"];
	[theWindow makeKeyAndOrderFront:self];
	[theWindow display];
	[theSoundView setDelegate:TheSoundManager];
 	[theSoundView setContinuous: [ThePreferencesManager ContinuousSelection]];		// For Continuous Sample Update
	[theSound setDelegate:TheSoundManager];
	[self TurnOnMenu:self];
	if (ThisSound==theSound) [self SoundChanged: ThisSound];	return self;
}

- Open:sender
{
    id theWindow, theScrollView, theSoundView, theSound;
	NXRect aRect, contentRect;
	id openpanel;
	char nameoffile[MAXSTRINGLENGTH];
	char const*filetype[2]={0,0};
	
    PanelCancelled=YES;
	filetype[0]="snd";
    openpanel=[OpenPanel new];
    [openpanel setDelegate:self];
	[openpanel allowMultipleFiles:NO];
    CurrentPanel=openpanel;
	[openpanel runModalForTypes:filetype];
	if (!PanelCancelled)
	{
		strcpy (nameoffile, [openpanel filename]);
			
		/* Stolen right out of the NeXT Concepts 2.0, p. 9-33 */
	
		[ThePreferencesManager SoundComingIn];
		[TheSoundTable NewWindowPos];
		NXSetRect(&aRect,[TheSoundTable WindowPosX],
					[TheSoundTable WindowPosY],600.0,250.0);
		theWindow=[Window newContent:&aRect
								style:NX_TITLEDSTYLE
								backing:NX_BUFFERED
								buttonMask:NX_ALLBUTTONS
								defer:NO];
		[theWindow setBackgroundGray:NX_WHITE];
		
		theScrollView=[ScrollView newFrame:&aRect];
		[theScrollView setVertScrollerRequired:NO];
		[theScrollView setHorizScrollerRequired:YES];
		
		[theWindow setContentView:theScrollView];
			
		contentRect=aRect;
		[ScrollView getContentSize:&(contentRect.size)
				forFrameSize:&(aRect.size)
				horizScroller:YES
				vertScroller:NO
				borderType:NX_NOBORDER];
	
		theSoundView = [UpdateSoundView newFrame:&contentRect];
			// my custom sound view.
		[theSoundView HeresTheEditController: TheEditController : [ThePreferencesManager ContinuousView]];
		[theSoundView notifyAncestorWhenFrameChanged:YES];
		theSound = [[Sound alloc] initFromSoundfile:nameoffile];

		[theSoundView setSound:theSound];
		[theSoundView setDisplayMode:[ThePreferencesManager MinMaxDisplay]];
		[theScrollView setDocView:theSoundView];

		[[theScrollView superview] setAutoresizeSubviews:YES];
		[[theScrollView superview] setAutosizing:
							NX_HEIGHTSIZABLE|NX_WIDTHSIZABLE];
	
		[TheSoundTable NewItem:theSound:theSoundView:theWindow:nameoffile:NO];

		[theWindow setTitleAsFilename:nameoffile];
		[theWindow setMiniwindowIcon: "SoundIcon"];
		[theWindow display];
		[theWindow setDelegate:self];
		[theWindow makeKeyAndOrderFront:self];
		[theSoundView setDelegate:TheSoundManager];
		[theSoundView setContinuous: [ThePreferencesManager ContinuousSelection]];
			// For Continuous Sample Update
		[theSound setDelegate:TheSoundManager];
		[self TurnOnMenu:self];
		}
	return self;
}

- Close:sender
{
	if (!([self CurrentWindow:self]==NULL))
		{[[self CurrentWindow:self] performClose:self];}
	return self;
}

- Quit:sender
{
   	Quitting=YES;
	[ThePreferencesManager SavePreferences];
	if ([TheSoundTable FindChangedSound])
		{
		DoQuit=NO;
		CurrentPanel=QuitPanel;
		[QuitPanel makeKeyAndOrderFront:self];
		[NXApp runModalFor:QuitPanel];
		if (DoQuit==YES) {[NXApp terminate:self];}
		Quitting=NO;
		}
	else [NXApp terminate:self];
	return self;
}

- Revert:sender
{
    char nameoffile[MAXSTRINGLENGTH];
	[TheSoundTable FindSound:[self CurrentSound:self]];
	strcpy (nameoffile, [TheSoundTable ReturnFileName]);
	[[self CurrentSound:self] initFromSoundfile: nameoffile];
	return self;
}

- SaveAll:sender
{
	BOOL DoSave=YES;
	BOOL SoundsLeft=YES;
    while (SoundsLeft)
		{
		SoundsLeft=[TheSoundTable FindChangedSound];
		if (SoundsLeft)
			{
			[[TheSoundTable ReturnWindow] makeKeyAndOrderFront:self];
			DoSave=([self Save]);
			if (!DoSave) 
				{
				SoundsLeft=NO;
				}
			}

		}
	return self;
}

- SaveAs:sender
{
	[self SaveAs];
	return self;
}

- Save:sender
{
	[self Save];
	return self;
}

- (BOOL) SaveAs
{
    id savepanel;
	char* type="snd";
	char nameoffile[MAXSTRINGLENGTH];
	
    PanelCancelled=YES;
	[TheSoundTable FindWindow:[self CurrentWindow:self]];
	savepanel=[SavePanel new];
	[savepanel setDelegate:self];
	[savepanel setRequiredFileType:type];
    CurrentPanel=savepanel;
	[savepanel runModal];
	strcpy (nameoffile, [savepanel filename]);
	if (!PanelCancelled)
		{
			[[TheSoundTable ReturnSound] writeSoundfile:nameoffile];
			[[TheSoundTable ReturnWindow] setTitleAsFilename:nameoffile];
			[[TheSoundTable ReturnWindow] setDocEdited:NO];
			[TheSoundTable SetFileName:nameoffile];
			[TheSoundTable SoundReset];
			return YES;
		}
	else
		{
			return NO;
		}
}

- (BOOL) Save
{
    char nameoffile[MAXSTRINGLENGTH];
	BOOL Untitled=[TheSoundTable ReturnUntitled];
	int SoundDataLength;
    [TheSoundTable FindWindow:[self CurrentWindow:self]];
	SoundDataLength=[[TheSoundTable ReturnSound] dataSize];
	strcpy (nameoffile, [TheSoundTable ReturnFileName]);
	if (SoundDataLength)
		{
		if (!Untitled)
			{
			[TheSoundTable FindSound:[self CurrentSound:self]];
			[[TheSoundTable ReturnSound] writeSoundfile:nameoffile];
			[[TheSoundTable ReturnWindow] setTitleAsFilename:nameoffile];
			[[TheSoundTable ReturnWindow] setDocEdited:NO];
			[TheSoundTable SetFileName:nameoffile];
			[TheSoundTable SoundReset];
			return YES;
			}
		else
			{
			return [self SaveAs];
			}
		}
	else 
		{
		[NXApp runModalFor:NoDataSavePanel];
		[[TheSoundTable ReturnWindow] setDocEdited:NO];
		[TheSoundTable SoundReset];
		return YES;
		}
}

- Print:sender
{
	if (!([self CurrentWindow:self]==NULL))
		{
		[TheSoundTable FindWindow:[self CurrentWindow:self]];
		[[TheSoundTable ReturnSoundView] printPSCode:self];
		}
	return self;
}

- CurrentWindow:sender
{
	id thestuff=[NXApp mainWindow];
	return thestuff;
}

- CurrentSound:sender
{
	if ([self CurrentWindow:self]==NULL) {return NULL;}
	else {
		  [TheSoundTable FindWindow:[self CurrentWindow:self]];
		  return [TheSoundTable ReturnSound];
		 }
}

- CurrentSoundView:sender
{
	if ([self CurrentWindow:self]==NULL) {return NULL;}
	else {
		  [TheSoundTable FindWindow:[self CurrentWindow:self]];
		  return [TheSoundTable ReturnSoundView];
		 }
}

- SoundChanged:ThisSound
{
   	float TheReduction;
	[TheSoundTable FindSound:ThisSound];
	TheReduction=[[TheSoundTable ReturnSoundView] reductionFactor];
	[TheSoundTable SoundEdited];
	[[TheSoundTable ReturnWindow] setDocEdited:YES];
	[[TheSoundTable ReturnWindow] makeKeyAndOrderFront:self];
	[[TheSoundTable ReturnSoundView] setSound:ThisSound];
	[[TheSoundTable ReturnSoundView] setReductionFactor: TheReduction];
	[TheEditController ChangeSelection:self];
	[TheEditController ChangeView:self];
	[TheEditController ZoomChanged:self];
	[TheInfoManager ChangeInfo: ThisSound];
	return self;
}


         /*----------------*USED INTERNALLY*-------------------*/

- SaveBeforeQuit:sender;
{
	BOOL SoundsLeft=YES;
	[NXApp stopModal];
	[QuitPanel close];
	while (SoundsLeft)
		{
		SoundsLeft=[TheSoundTable FindChangedSound];
		if (SoundsLeft)
			{
			[[TheSoundTable ReturnWindow] makeKeyAndOrderFront:self];
			DoQuit=([self Save]);
			if (!DoQuit) 
				{
				SoundsLeft=0;
				}
			}
		}
	return self;
}

- DontSaveBeforeQuit:sender;
{
	[NXApp stopModal];
	[QuitPanel close];
	DoQuit=YES;
	return self;
}

- CancelSaveBeforeQuit:sender;
{
	[NXApp stopModal];
	[QuitPanel close];
	DoQuit=NO;
	return self;
}

- SaveBeforeClose:sender
{
	id savepanel;
	char* type="snd";
	char nameoffile[MAXSTRINGLENGTH];
	BOOL Untitled=[TheSoundTable ReturnUntitled];
    int SoundDataLength;
    [NXApp stopModal];
	[ClosePanel close];
	
	[TheSoundTable FindWindow:[self CurrentWindow:self]];
	SoundDataLength=[[TheSoundTable ReturnSound] dataSize];
	strcpy (nameoffile, [TheSoundTable ReturnFileName]);
	if (SoundDataLength)
		{
		if (!Untitled)
			{
			[[TheSoundTable ReturnSound] writeSoundfile:nameoffile];
			[[TheSoundTable ReturnWindow] setTitleAsFilename:nameoffile];
			[[TheSoundTable ReturnWindow] setDocEdited:NO];
			[TheSoundTable SetFileName:nameoffile];
			[TheSoundTable SoundReset];
			DoClose=YES;
			}
		else
			{
			PanelCancelled=YES;
			savepanel=[SavePanel new];
			[savepanel setDelegate:self];
			[savepanel setRequiredFileType:type];
			CurrentPanel=savepanel;
    		[savepanel runModal];
			strcpy (nameoffile, [savepanel filename]);
			if (!PanelCancelled)
				{
					[[TheSoundTable ReturnSound] writeSoundfile:nameoffile];
					[[TheSoundTable ReturnWindow] setTitleAsFilename:nameoffile];
					[[TheSoundTable ReturnWindow] setDocEdited:NO];
					[TheSoundTable SetFileName:nameoffile];
					[TheSoundTable SoundReset];
					DoClose=YES;
				}
			else DoClose=NO;
			}
		}
		else
		{
		[NXApp runModalFor:NoDataSavePanel];
		[[TheSoundTable ReturnWindow] setDocEdited:NO];
		[TheSoundTable SoundReset];
		DoClose=YES;
		}
	return self;
}

- DontSaveBeforeClose:sender
{
	[NXApp stopModal];
	[ClosePanel close];
	
	DoClose=YES;
	return self;
}

- CancelSaveBeforeClose:sender
{
	[NXApp stopModal];
	[ClosePanel close];
	
	DoClose=NO;
	return self;
}

- TurnOnMenu:sender
{
	[SaveButton setEnabled:YES];
	[SaveAsButton setEnabled:YES];
	[SaveAllButton setEnabled:YES];
	[RevertButton setEnabled:YES];
	[CloseButton setEnabled:YES];
	[TheModuleController TurnOnMenu:self];
	[TheEditController TurnOnMenu:self];
	return self;
}

- TurnOffMenu:sender
{
	[SaveButton setEnabled:NO];
	[SaveAsButton setEnabled:NO];
	[SaveAllButton setEnabled:NO];
	[RevertButton setEnabled:NO];
	[CloseButton setEnabled:NO];
	[TheModuleController TurnOffMenu:self];
	[TheEditController TurnOffMenu:self];
	return self;
}


-  CheckInformation:sender;
{
	if ([self CurrentSound:self]==NULL)
		{
		[self TurnOffInformation:self];
		}
	else
		{
		[self TurnOnInformation:self];
		}
	return self;
}


- TurnOffInformation:sender;
{
	[TheInfoManager NoWindow];
	[TheEditController ResetSelection:self];
	return self;
}



- TurnOnInformation:sender;
{
	[TheInfoManager ChangeInfo: [self CurrentSound:self]];
	[TheEditController ChangeSelection:self];
	[TheEditController ChangeView:self];
	return self;
}



- NoDataSaveOkay:sender
{
	[NXApp stopModal];
	[NoDataSavePanel close];
	return self;
}


- DoPageLayout:sender
{
	[NXApp runPageLayout:self];
	return self;
}


/*IMPLEMENTED AS A WINDOW DELEGATE*/


- windowDidResize:sender
{
	NXSize ThisSize;
	NXRect ThisRect;
	[TheSoundTable FindWindow:sender];
	
	[[[TheSoundTable ReturnWindow] contentView] getContentSize: &ThisSize];
	[[TheSoundTable ReturnSoundView] getBounds:&ThisRect];
	
	[[TheSoundTable ReturnSoundView] sizeTo: ThisRect.size.width : ThisSize.height];
				
	[TheEditController ZoomChanged:self];
	return self;
}

- windowDidBecomeKey:sender
{
	if (!(sender==CurrentPanel))
		{
		[Console makeKeyWindow];
		[TheSoundTable FindWindow:sender];
		[TheInfoManager ChangeInfo:[TheSoundTable ReturnSound]];
		[TheEditController ZoomChanged:self];
		}
	[TheEditController ChangeSelection:self];
	[TheEditController ChangeView:self];
	return self;
}

- windowDidBecomeMain:sender
{
	[TheEditController ChangeSelection:self];
	[TheEditController ChangeView:self];
	return self;
}

- windowWillClose:sender
{

	id TheWindow;
	if (!Quitting)
		{
		[sender becomeMainWindow];
		[sender orderFront:self];
		[TheSoundTable FindWindow:[self CurrentWindow:self]];
		TheWindow=[TheSoundTable ReturnWindow];
		if ([TheSoundTable IsSoundChanged])
			{
			[ClosePanel makeKeyAndOrderFront:self];
			[NXApp runModalFor:ClosePanel];
			if (DoClose==YES)
				{
				[TheSoundTable FindWindow:TheWindow];
				[[TheSoundTable ReturnSound] free];
				[TheSoundTable Delete];
				}
			else return NULL;
			}
		else
			{
			[TheSoundTable FindWindow:TheWindow];
			[[TheSoundTable ReturnSound] free];
			[TheSoundTable Delete];
			}
		printf("SoundTable number:  %d", [TheSoundTable Total]);
		if (!([TheSoundTable Total]-1))
			{
			[self TurnOffMenu:self];
			}
		}
	return self;
}

- WindowDidUpdate:sender
{
	[TheEditController ChangeSelection:self];
	[TheEditController ChangeView:self];
	return self;
}

- WindowDidResize:sender
{
	[TheEditController ChangeView:self];
	return self;
}


/* IMPLEMENTED AS A DELEGATE OF SAVEPANEL AND OPENPANEL */

- (BOOL) panelValidateFilenames:sender
{
	PanelCancelled=NO;
	return YES;
}


/* IMPLEMENTED AS A DELEGATE OF APPLICATION */


- appDidInit:sender
{
	[Console setFloatingPanel:YES];
	[Console makeKeyWindow];
	return self;
}


- appDidBecomeActive:sender
{
	[Console makeKeyWindow];
	return self;
}


- appDidUnhide:sender
{
	[Console makeKeyWindow];
	return self;
}


- appDidUpdate:sender
{
	[Console makeKeyWindow];
	return self;
}


@end
